package ihm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import metier.Film;
import dao.DaoProjetCinema;

public class FenetreAjoutFilm extends JInternalFrame implements ActionListener
{
	private JTextField txtTitre;
	private JTextField txtDuree;
	private JTextField txtDateSortie;
	private JTextField txtNbreSemaine;
	private JLabel lblTitre;
	private JLabel lblDure;
	private JLabel lblDateDeSortie;
	private JLabel lblNombreDeSemaines;
	private JButton btnAjouter;
	private Vector<Film> lesFilms;


	/**
	 * Create the frame.
	 */
	public FenetreAjoutFilm(Vector <Film> lesFilms)
	{
		getContentPane().setLayout(null);
		
		this.txtTitre = new JTextField();
		this.txtTitre.setBounds(117, 35, 181, 20);
		getContentPane().add(txtTitre);
		this.txtTitre.setColumns(10);
		
		this.txtDuree = new JTextField();
		this.txtDuree.setBounds(234, 62, 62, 20);
		getContentPane().add(txtDuree);
		this.txtDuree.setColumns(10);
		
		this.txtDateSortie = new JTextField();
		this.txtDateSortie.setBounds(236, 93, 62, 20);
		getContentPane().add(txtDateSortie);
		this.txtDateSortie.setColumns(10);
		
		this.txtNbreSemaine = new JTextField();
		this.txtNbreSemaine.setBounds(236, 124, 62, 20);
		getContentPane().add(txtNbreSemaine);
		this.txtNbreSemaine.setColumns(10);
		
		this.lblTitre = new JLabel("Titre : ");
		this.lblTitre.setBounds(61, 38, 46, 14);
		getContentPane().add(lblTitre);
		
		this.lblDure = new JLabel("Dur\u00E9e :");
		this.lblDure.setBounds(61, 65, 46, 14);
		getContentPane().add(lblDure);
		
		this.lblDateDeSortie = new JLabel("Date de sortie :");
		this.lblDateDeSortie.setBounds(61, 96, 102, 14);
		getContentPane().add(lblDateDeSortie);
		
		this.lblNombreDeSemaines = new JLabel("Nombre de semaines :");
		this.lblNombreDeSemaines.setBounds(61, 124, 188, 20);
		getContentPane().add(lblNombreDeSemaines);
		
		this.btnAjouter = new JButton("Ajouter");
		this.btnAjouter.addActionListener(this);
		this.btnAjouter.setBounds(307, 179, 91, 23);
		getContentPane().add(btnAjouter);
		
		this.lesFilms = lesFilms;
	}

	
	public void actionPerformed(ActionEvent evt) 
	{
		Film leFilm;
		int nb;
		
		if (evt.getSource() == this.btnAjouter)
		{
			if (this.txtTitre.getText().isEmpty() || this.txtNbreSemaine.getText().isEmpty() || this.txtDuree.getText().isEmpty() || this.txtDateSortie.getText().isEmpty())
				JOptionPane.showMessageDialog(null, "Tous les champs sont obligatoires !");
			else
			{
				leFilm = new Film(this.txtTitre.getText(), Integer.parseInt(this.txtDuree.getText()), Date.valueOf(this.txtDateSortie.getText()) , Integer.parseInt(this.txtNbreSemaine.getText()));	
			
				nb = DaoProjetCinema.ajouterFilm(leFilm);
				if (nb == 0)
					JOptionPane.showMessageDialog(null, "Ce film existe déjà !");
				else
				{
					JOptionPane.showMessageDialog(null, "Film ajouté.");
					this.lesFilms.add(leFilm);
				}
			}
		}
	}
}